//
//  ConnectionDeviceViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/8.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "Common.h"
#import <GeTuiSdk.h>
#import "UserAlerts.h"
#import "JlUserManager.h"
#import "SocketManager.h"
#import "LoginViewController.h"
#import "ConnectionTableViewCell.h"
#import "ServiceZoneViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import "ConnectionDeviceViewController.h"

#define kCellHeight 23
#define kDetermineBottom 8
#define kDetermineHeight 43
@interface ConnectionDeviceViewController () <UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *dateSource;
}

@end

@implementation ConnectionDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设备搜索";
    if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
        [self showNavBarWithRightTitle:@"返回"
                        andRightAction:@selector(returnloginPage)
                         andLeftAction:@selector(homeControllerBackTo)
                     andRightBtnIsShow:YES];
    } else {
        [self showNavBarWithRightTitle:nil
                        andRightAction:nil
                         andLeftAction:@selector(homeControllerBackTo)
                     andRightBtnIsShow:NO];
    }
   
}

- (void)returnloginPage {
    [GeTuiSdk unbindAlias:[JlUserManager shareInstance].userInfo.userid];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JiuLeOfflineMode];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserInfoUserDefault];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDataPassWord];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserInfoUserDefault];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JiuLeIsConnection];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:JiuLeUserName];
    [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:@"push"];
    LoginViewController *login = getStoryOfControllerInstance(@"LoginViewController");
    self.view.window.rootViewController = login;

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (![SocketManager sharedInstance].socket.isConnected) {
        [self connect];
    } else {
        [[SocketManager sharedInstance] writeActiveGet];
    }
}

- (void)connect {
    //192.168.1.1
    //6666
    [CLUIBaseViewController connectWifi];
}

- (IBAction)connectionEvent:(id)sender {
    if (![SocketManager sharedInstance].socket.isConnected) {
        [[UserAlerts instance] showErrorMsg:@"提示" message:@"未连接久乐表，请连接成功后点击确认连接!"];
        [_beginConnection setTitle:@"确认连接" forState:UIControlStateNormal];
        postNotificationWithObject(UserDeviceConnect, @(NO));
    } else {
//        postNotification(UserDeviceConnect);
        postNotificationWithObject(UserDeviceConnect, @(YES));
        [_beginConnection setTitle:@"已连接" forState:UIControlStateNormal];
        [[SocketManager sharedInstance]writeActiveGet];
    }
}

- (IBAction)determineEvent:(id)sender {
    //TODO:Nothing to do
}

- (void)beginSearchDevice {
    if (dateSource==nil || dateSource.count == 0) {
        dateSource = [NSMutableArray new];
    }
    //TODO:搜索设备的操作
    NSInteger count = (arc4random() % 10) + 1;
    for (int i = 0 ; i < count ; i++) {
        NSString *str = @"xcq";
        str = [str stringByAppendingFormat:@"%d",i];
        [dateSource addObject:str];
    }
    [self adjustTableHeight];
}

- (void)adjustTableHeight {
    NSLog(@"dateSource count : %zd",dateSource.count);
    CGFloat y = CGRectGetMinY(_wifiTable.frame);
    CGFloat height = CGRectGetHeight(self.view.frame) - y;
    CGFloat tableHeiht = height - 2*kDetermineBottom - kDetermineHeight;
    CGFloat trueTableHeight = dateSource.count * kCellHeight;
    if (trueTableHeight < tableHeiht) {
        CGFloat difference = tableHeiht - trueTableHeight;
        _DetermineTopLayout.constant += difference;
    }
    if (self.wifiTable.delegate==nil) {
        self.wifiTable.delegate = self;
        self.wifiTable.dataSource = self;
        self.wifiTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    [self.wifiTable reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dateSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ConnectionTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"ConnectionTableViewCell" owner:self options:nil] lastObject];
    cell.name.text = [dateSource objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kCellHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
